var props = require('./props')();
var GameData = require('../../module/GameData')();

cc.Class({
    extends: cc.Component,

    properties: {
        PropsAtlas: cc.SpriteAtlas,
        props_prefab: cc.Prefab,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {

        GameData.All_block = [];
        this.props_pool = new cc.NodePool();

        this.initMap();

    },
    /**
     * time 2019/8/20
     * author:zzw
     * dsc: 初始化地图
     */
    initMap() {
        var self = this;
        cc.loader.loadRes("Bankheist.json", function (err, object) {
            if (err) {
                console.log(err);
                return;
            }
            let h = object.json.height;
            let w = object.json.width;
            let data = object.json.data;
            let tileLen = 45;

            let originRow = (h - 1) / 2;
            let originCol = (w - 1) / 2;
            for (let i = 0; i < data.length; i++) {
                var row = Math.floor(i / w);
                var col = i % w;
                var x = (row - originRow) * tileLen;
                var y = (col - originCol) * tileLen;
                let _data = {
                    x: x,
                    y: y,
                    row: row,
                    col: col,
                    key: null,
                    MaxRow: w,
                };
                let _key = null;
                switch (data[i]) {
                    case 1 :
                        _key = "rock";
                        break;
                    case 11:
                    case 12:
                    case 62:
                    case 63:
                    case 64:
                        _key = "grass";
                        break;
                    case 16:
                    case 53:
                    case 57:
                    case 58:
                    case 59:
                    case 60:
                    case 61:
                    case 65:
                        _key = "wall";
                        break;
                    case 17:
                    case 54:
                    case 56:
                        _key = "box";
                        break;

                }

                if (_key) {
                    _data.key=_key;
                    props.createProps(self.props_pool, self.props_prefab, self.PropsAtlas, _data, self.node, GameData.All_block);

                } else {
                    GameData.All_block[row * w + col] = _data;
                }
            }
        })
    },

    start() {

    },

    // update (dt) {},
});
